package com.novunda.platform.web;

import com.novunda.platform.common.context.SpringContextHolder;
import com.novunda.platform.common.utils.StringUtil;
import com.novunda.platform.entities.User;
import com.novunda.platform.security.LoginTypeEnum;
import com.novunda.platform.security.NovUsernamePasswordToken;
import com.novunda.platform.services.UserService;
import com.novunda.tca.tdk.domain.ResourceDataInfo;
import com.novunda.tca.tdk.servlet.NovundaServlet;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 注册回调
 * Created by YJY on 2016/9/21.
 */
@WebServlet("/esc/rs")
public class RegisterServlet extends NovundaServlet {

    private static final long serialVersionUID = 3555221554515593156L;

    private final Logger logger = Logger.getLogger(getClass());

    @Override
    protected void doHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ResourceDataInfo resourceDataInfo) {
        String qrdata = resourceDataInfo.getQrdata();
        //判断qrdata是不是空
        if (!StringUtil.isEmpty(resourceDataInfo.getQrdata())) {
            //获取户名
            String username = qrdata.split(":::")[0];
            UserService userService = SpringContextHolder.getBean(UserService.class);
            //获取用户信息
//            User user = userService.findByLoginName(username);
            //判断用户信息是不是空
//            if (user != null) {
//                user.setState(1);
//                userService.saveUser(user);
//                SecurityUtils.getSubject().login(new NovUsernamePasswordToken(username, user.getPassword().toCharArray(), httpServletRequest.getRemoteHost(), LoginTypeEnum.QRCODE));
//                RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher("/");
//                try {
//                    dispatcher .forward(httpServletRequest, httpServletResponse);
//                } catch (ServletException | IOException e) {
//                    logger.info(e.getMessage());
//                }
//            }
        }
    }
}
